package com.joysuch.wwyt.mh.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.common.enums.AppBusinessNoticeTypes;
import com.joysuch.wwyt.common.repository.CommonIndexNotifyDao;
import com.joysuch.wwyt.common.service.CommonIndexNotifyService;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.mh.entity.MhMajorHazardTaskAssign;
import com.joysuch.wwyt.mh.enums.TaskStatus;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardTaskAssignMapper;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardTaskMapper;
import com.joysuch.wwyt.mh.service.MhMajorHazardTaskAssignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zqw
 * @since 2022-05-24
 */
@Service
public class MhMajorHazardTaskAssignServiceImpl extends ServiceImpl<MhMajorHazardTaskAssignMapper, MhMajorHazardTaskAssign> implements MhMajorHazardTaskAssignService {

    @Autowired
    private MhMajorHazardTaskAssignMapper mhMajorHazardTaskAssignMapper;
    @Autowired
    private CommonIndexNotifyService indexNotifyService;
    @Autowired
    private MhMajorHazardTaskMapper mhMajorHazardTaskMapper;
    @Autowired
    private CommonIndexNotifyDao commonIndexNotifyDao;

    @Override
    public void deleteById(Long id) {
        MhMajorHazardTaskAssign assign = getById(id);
        if (assign != null) {
            assign.setDeleteFlag(DeleteFlag.DELETED.getCode());
            saveOrUpdate(assign);
            commonIndexNotifyDao.deletePlanByTaskIds(Lists.newArrayList(id), AppBusinessNoticeTypes.EMERGENCY_DRILL_PLAN.getType());
        }
    }

    @Override
    public void deleteByMhId(Long id) {
        List<Long> assignIdList = mhMajorHazardTaskMapper.findAssignIdsByMhId(id);
        if(CollectionUtils.isNotEmpty(assignIdList)){
            mhMajorHazardTaskAssignMapper.deleteByIds(assignIdList);
            commonIndexNotifyDao.deletePlanByTaskIds(assignIdList, AppBusinessNoticeTypes.EMERGENCY_DRILL_PLAN.getType());
        }
    }

    @Override
    public void updateDoneStatus(List<Long> taskIds, Long userId, Integer doneStatus) {
        mhMajorHazardTaskAssignMapper.updateStatusByTaskIdsAndUserId(taskIds, userId, doneStatus);
    }

    @Override
    public List<MhMajorHazardTaskAssign> batchSaveOverdueTaskAssigen(List<Long> taskIds) {
        List<MhMajorHazardTaskAssign> taskAssigns = mhMajorHazardTaskAssignMapper.findByTaskIds(taskIds,null);
        if (!CollectionUtils.isEmpty(taskAssigns)) {
            taskAssigns.forEach(a -> {
                a.setDoneStatus(TaskStatus.OVERDUE.getStatus());
            });
            saveOrUpdateBatch(taskAssigns);
        }
        return taskAssigns;
    }

}
